namespace SharpTalk.Client.WpfApplication.ViewModels
{
    using Core.Base;
    using Views;
    using System.Windows.Input;
    using System.Reflection;
    using System.Diagnostics;

    /// <summary>
    /// Abstract base ViewModel class
    /// </summary>
    /// <typeparam name="T">View interface contract</typeparam>
    public abstract class ViewModelBase<T> : PropertyChangedBase
        where T : IView
    {
        private readonly T _view;
        private readonly ICommand _validateCommand = null;
        /// <summary>
        /// Private constructor. do not use.
        /// </summary>
        private ViewModelBase(){}
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="view">The view associated</param>
        protected ViewModelBase(T view)
        {
            _view = view;
            TraceProvider = App.Trace;
        }
        /// <summary>
        /// Obtains the view associated
        /// </summary>
        protected internal T View { get { return _view; } }
        /// <summary>
        /// Obtains the validation command
        /// </summary>
        public virtual ICommand ValidateCommand
        {
            get { return _validateCommand ?? new BindableCommand<object>(OnValidate); }
        }

        internal virtual void OnValidate(object value)
        {
            if (ShouldTrace(TraceEventType.Verbose)) TraceMethodCall(MethodBase.GetCurrentMethod());
            View.DialogResult = true;
            View.Close();
        }
    }
}